(function( factory ) {
	define( ["static/assets/datatables/1.10.15/js/jquery", "datatables.bootstrap","nicescroll","layer"], factory );
}(function( $ ) {
	function language(){
    	return {
            processing: '<div class="panel-overlay-content unselectable"><span class="panel-overlay-icon text-dark"><i class="fa fa-spinner fa-2x fa-spin"></i></span><h4 class="panel-overlay-title">数据加载中...</h4></div>',
            lengthMenu: "显示 _MENU_ 项结果",
            zeroRecords: "没有匹配结果",
            info: "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            infoEmpty: "显示第 0 至 0 项结果，共 0 项",
            infoFiltered: "(由 _MAX_ 项结果过滤)",
            infoPostFix: "",
            search: "搜索:",
            searchPlaceholder: "搜索...",
            url: "",
            emptyTable: "暂无数据",
            loadingRecords: "载入中...",
            infoThousands: ",",
            paginate: {
                first: "首页",
                previous: "上页",
                next: "下页",
                last: "末页"
            }
        }
    }
    
    function sDom(){
    	return "<'row'<'col-sm-12'f>>" + //搜索框	
    		   "<'row'<'col-sm-12'tr>>" + //表格主体
			   "<'row'<'dataTables-length-wrapper'l><'dataTables-info-wrapper'i><'dataTables-paginate-wrapper'p>>" //分页布局
    }
    
    function ajaxFun(options , data, callback, settings){
		 var dataFun = options.ajax.data;
		 var param = {};
		 if(options.serverSide){
			 param.pageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
			 param.pageIndex = data.start;//开始的记录序号
			 param.pageNum = (data.start / data.length)+1;//当前页码
		 }
		 if(dataFun){
			if(dataFun(param) == false){
				return;
			}
		 }
		 $.ajax({
			url: ajaxBaseUrl + options.ajax.url,
			data:param,
			cache: false,
			type:options.ajax.type?options.ajax.type:"get",
			async : options.ajax.async != null && options.ajax.async !=undefined ? options.ajax.async : true,
			success:options.ajax.success ? options.ajax.success : function(resp){
				if(resp.head.code != "SUCCESS"){
					layer.msg(resp.head.message);
					return;
				}
				var result = resp.data;
				//封装返回数据
      		    var returnData = {};
      			//返回数据全部记录
      		    returnData.recordsTotal = result.total ? result.total : 0;
      			//后台不实现过滤功能，每次查询均视作全部结果
      		    returnData.recordsFiltered = result.total ? result.total : 0 ;
      			//返回的数据列表
      		    returnData.data = result.list ? result.list : [];
      		    //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
      		    //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
      		    callback(returnData);
      			if(options.ajax.listen){
      				options.ajax.listen(result);
      			}
			}
		}); 
    }
    
    function columnsFun(columns) {
    	for(var i = 0 ; i < columns.length ; i++){
    		var column = columns[i];
    		if(column != null && column != undefined && !column.render){
    			column.render = function(data, type, row) {
    				if(data == null || data == undefined){
    					return "";
    				}
    				if(column.escape == null ||  column.escape == undefined || column.escape == true){
						return "<span title='"+data+"'>"+html2Escape(data)+"</span>";
					}
    				return "<span title='"+data+"'>"+data+"</span>";
    	        };
    	        continue;
    		}
    	}
    	return columns;
    }

    $.fn.loadDataTable = function(options){
    	//定义布局
		options.sDom = options.sDom ? options.sDom : sDom();
		//默认关闭排序
		options.ordering = options.ordering != null && options.ordering !=undefined ? options.ordering : false;
		//去掉搜索框
		options.bFilter = options.bFilter != null && options.bFilter == undefined ? options.bFilter : false;   
		//翻页功能,默认开启
		options.bPaginate = options.bPaginate != null && options.bPaginate != undefined ? options.bPaginate : true; 
		//页面显示条数
		options.aLengthMenu = options.aLengthMenu ? options.aLengthMenu : [10, 15, 20, 25];
		//打开后台分页 
		options.serverSide = options.serverSide != null && options.serverSide != undefined ? options.serverSide : true;
		//是否每次请求都重新生成表格 	
		options.destroy= options.destroy!=null && options.destroy!=undefined ? options.destroy : false;
		//自适应宽度
		options.bAutoWidth = options.bAutoWidth != null && options.bAutoWidth != undefined ? options.bAutoWidth : false;;
		if(options.ajax){
			var dataFun = options.ajax.data;
			if(dataFun){
				var param = {};
				if(dataFun(param) == false){
					return;
				}
			}
			var opts = {};
			opts.fixedHeader = options.fixedHeader;
			opts.serverSide = options.serverSide;
			opts.ajax = options.ajax;
			options.ajax = function (data, callback, settings){
				ajaxFun(opts , data, callback, settings);
			}
		}
		if(options.columns){
			options.columns = columnsFun(options.columns);
		}
    	options.language = options.language ? options.language :language()
    	return this.DataTable(options);
    }
	
}));
	
